class ListNode {
   int val;
   ListNode next = null;
   public ListNode(int val) {
     this.val = val;
   }
 }

public class HuiWenListNode {
    public ListNode reverse(ListNode head){
        ListNode cur1=head;
        ListNode cur2=null;
        while(cur1!=null){
            ListNode next=cur1.next;
            cur1.next=cur2;
            cur2=cur1;
            cur1=next;
        }
        return cur2;
    }
    public boolean isPail (ListNode head) {
        // write code here
        if(head==null){
            return true;
        }
        ListNode createNode=new ListNode(-1);
        ListNode p1=createNode;
        ListNode p2=head;
        while(p2!=null){
            p1.next=new ListNode(p2.val);
            p1=p1.next;
            p2=p2.next;
        }
        p1=createNode.next;
        p2=reverse(head);
        while(p1!=null&&p2!=null){
            if(p1.val!=p2.val){
                return false;
            }
            p1=p1.next;
            p2=p2.next;
        }
        return p1==null&&p2==null;
    }
}
